package com.itheima.test5;

import java.io.*;
import java.util.ArrayList;

/**
 * @Author:gy
 * @Date: 2025/08/31 16:08
 * @Description:
 */
public class Demo3 {
    public static void main(String[] args) throws Exception {
        // 姓名  年龄   学号   创建3个学生对象存到集合中  序列化到本地  最后在反序列化到控制台
        Student student1 = new Student("小帅",25,"heima001");
        Student student2 = new Student("小美",18,"heima002");
        Student student3 = new Student("小化",23,"heima003");
        //将这三个人存到集合中
        ArrayList<Student> al = new ArrayList<>();
        al.add(student1);
        al.add(student2);
        al.add(student3);
        //序列化  对象写到硬盘
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("Day11\\g.txt"));
        oos.writeObject(al);
        oos.close();
        //反序列化  硬盘上的字节码文件读取成对象
        ObjectInputStream ois = new ObjectInputStream(new FileInputStream("Day11\\g.txt"));
        ArrayList<Student> als = (ArrayList<Student>) ois.readObject();
        for (Student student : als) {
            System.out.println(student);
        }
    }
}
